/**
 * @class   WeatherWorker
 * @brief   
 *
 *
 *
 *
 *
 *
 *
 ****************************************************************************
 * @version $Id: WeatherWorker.h 2804 2010-07-27 04:39:07Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Service_Weather_Impl_H__
 #define __WOSH_Service_Weather_Impl_H__

 #include <core/BundleGenericWorker.h>
 #include <core/Containers.h>

 #include "WeatherSource.h"
 

using namespace wosh;

class WeatherWorker;

class IWeatherWorker {
	public:
		virtual void updatedWeather( const WeatherInfo* newInfo, double similarity, WeatherWorker* source ) = 0;
		virtual void updatedForecast( const std::vector<WeatherInfo*>& newForecast, WeatherWorker* source ) = 0;

	public:
		virtual ~IWeatherWorker() { }
};

typedef ListT<WeatherSource*> tWeatherSourceList; 
typedef MapT<int64,WeatherInfo*> tInt64WeatherInfoMap;
typedef ListT<WeatherInfo*> tWeatherInfoList; 


class WeatherWorker : public BundleGenericWorker
 {

	public:
		/**
		 * @brief   Default constructor. Init vars.
		 * @param bundle [in] the parent bundle, see BundleGenericWorker interface
		 */
		WeatherWorker( BundleGeneric& bundle );
		/**
		 * @brief   Deconstructor. If thread is stll running, it will be stopped.
		 */
		virtual ~WeatherWorker();


	public:
		WRESULT addSource( WeatherSource* source );
		
		int updateHistory();

		void setListener( IWeatherWorker* ptr )	{ this->listener = ptr;}


/** @name Setters
 * @{
 ******************************************************************************/
 	public:

//@}
/** @name Getters
 * @{
 ******************************************************************************/
 	public:
		const tWeatherSourceList& getSources() const	{ return this->sources; }
		tWeatherSourceList& getSources()				{ return this->sources; }

		WeatherInfo& getWeather()						{ return this->wCurrentInfo; }
		MutexRW & getWeatherMux()						{ return this->wCurrentInfoMux; }

		const tInt64WeatherInfoMap& getForeCast() const	{ return this->forecast; }
		tInt64WeatherInfoMap& getForeCast()				{ return this->forecast; }

		const tWeatherInfoList& getHistory() const		{ return this->history; }
		tWeatherInfoList& getHistory()					{ return this->history; }

//@}
	protected:
		void mergeForeCast( std::vector<WeatherInfo*>& newForecast );

		void runThread();

	protected:
		tWeatherSourceList sources;

		WeatherInfo wCurrentInfo;
		MutexRW wCurrentInfoMux;

		tInt64WeatherInfoMap forecast;

		tWeatherInfoList history;
		int historyMax;

		IWeatherWorker* listener;


}; // class def

#endif //__WOSH_Service_Weather_Impl_H__
